import config from '@/config/config.js';
import axios from "axios";


const defectRequest={
    loadDefects(defect,page=1,limit=10,callBack){
        let url = config.qualityBaseUrl + "/defect/list?page="+page + "&limit="+limit;
        if(defect){
            if(defect.defectDesc){
                url += "&defectDesc=" + defect.defectDesc;
            }

            if(defect.inspectionType){
                url += "&inspectionType="+defect.inspectionType;
            }
            if(defect.defectLevel){
                url += "&defectLevel="+defect.defectLevel;
            }
        }

        // 发送请求
        axios.get(url)
            .then(res=>{
                // 回调函数是从外面传入的，我们要将我们请求得到的结果传回到回调函数中.
                // 由于axiso会对结果进行一个封装，所以我们这里传入回调函数的数据直接就是res.data
                // 我们在外面获取的数据就是我们后端响应的数据本身。
                callBack(res.data);
            })



    },


    addDefect(defect,callBack){
        console.log(defect)
        let url = config.qualityBaseUrl + "/defect/add"
        console.log(url)
        axios.post(url,defect)
            .then(res=>{
                callBack(res.data);
            })
    },


    selectDefectById(defectId,callBack){
        let url = config.qualityBaseUrl + "/defect/selectDefectById?defectId=" + defectId;
        axios.get(url)
            .then(res=>{
                callBack(res.data);
            })
    },

    updateDefect(defect,callBack){
        console.log(defect)
        let url = config.qualityBaseUrl + "/defect/update"
        console.log(url)
        axios.put(url,defect)
            .then(res=>{
                callBack(res.data);
            })
    },

    deleteDefect(defectId,callBack){
        let url = config.qualityBaseUrl + "/defect/delete?defectId=" + defectId
        axios.put(url)
            .then(res=>{
                callBack(res.data);
            })
    },


    deletes(defectIds,callBack){
        let url;
        if (defectIds){
            defectIds.forEach(taskId=> url = config.qualityBaseUrl + "/defect/deletes");
        }
        axios.put(url,defectIds)
            .then(res=>{
                callBack(res.data);
            })
    }






}


export default defectRequest;